package erp.education.server.beans.implementation.setup.school;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import erp.education.client.setup.school.BranchToCourseRequest;
import erp.education.server.beans.implementation.BaseService;
import erp.education.server.beans.implementation.ResultResponse;
import erp.education.server.beans.implementation.ServiceObject;
import erp.education.server.beans.interfaces.setup.school.BranchToCourseServiceI;
import erp.education.server.beans.interfaces.user.transaction.ServiceObjectI;
import erp.education.server.db.entites.setup.school.BranchToCourse;
import erp.education.server.db.exceptions.DbExceptions;

@Component("branchToCoursesService")
public class BranchToCoursesService extends BaseService implements
		BranchToCourseServiceI {

	@Override
	public ServiceObjectI branchsToCourseLoadSevice(
			BranchToCourseRequest branchRequest) throws DbExceptions {
		ServiceObjectI response = new ServiceObject();
		ResultResponse holder = new ResultResponse();
		List result = new ArrayList();
		result.add(branchsDao.getBranchList());
		result.add(coursesDao.getCourses());

		holder.setResultList(result);
		response.setResponseObject(holder);
		return response;
	}

	@Override
	public ServiceObjectI saveUpdateBranchToCourseDetails(
			BranchToCourseRequest branchRequest) throws DbExceptions {
		ServiceObjectI response = new ServiceObject();
		ResultResponse holder = new ResultResponse();
		List result = new ArrayList();
		BranchToCourse branchCourse = null;
		for (Long branchId : branchRequest.getSelectedBranchs()) {
			for (Long courseId : branchRequest.getSelectedCourses()) {
				branchCourse = null;
				branchCourse = branchToCourseDao.getBranchToCourse(branchId,
						courseId);
				if (null == branchCourse) {
					branchCourse = new BranchToCourse();
					branchCourse.setBranchId(branchId);
					branchCourse.setCourseId(courseId);
					branchToCourseDao.merge(branchCourse);
				}
			}

		}
		holder.setResultList(result);
		response.setResponseObject(holder);
		return response;
	}
}
